package de.tum.in.msspp.message;

import java.io.IOException;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xmlpull.v1.XmlSerializer;

import android.content.Context;
import android.util.Log;
import android.util.Xml;

public class MessageXMLParser extends DefaultHandler {
	
	Message message;
	String tempVal;
	Context context;
	
	public MessageXMLParser(Context c){
		super();
		context = c;
	}
	
	public Message parseXML(InputStream input) {

		//get a factory
		SAXParserFactory spf = SAXParserFactory.newInstance();
		try {

			//get a new instance of parser
			SAXParser sp = spf.newSAXParser();

			//parse the file and also register this class for callbacks
			sp.parse(input, this);

		}catch(SAXException se) {
			Log.i("Parser", "Parser Error");
			se.printStackTrace();
		}catch(ParserConfigurationException pce) {
			Log.i("Parser", "Parser Error");
			pce.printStackTrace();
		}catch (IOException ie) {
			Log.i("Parser", "Parser Error");
			ie.printStackTrace();
		}
		Log.i("Parser", "Message Data: " + message.getData());
		return message;
	}
	
	//Event Handlers
	/**
	 * Found new xml-start-tag
	 */
	public void startElement(String uri, String localName, String qName,
		Attributes attributes) throws SAXException {
		//reset
		tempVal = "";
		if(qName.equalsIgnoreCase("message")){
			message = new Message();
		}
	}


	public void characters(char[] ch, int start, int length) throws SAXException {
		tempVal = new String(ch,start,length);
	}

	/**
	 * found xml-end-tag
	 */
	public void endElement(String uri, String localName,
		String qName) throws SAXException {

		if (qName.equalsIgnoreCase("from")) {
			message.setSender(tempVal);
		}else if (qName.equalsIgnoreCase("type")) {
			message.setType(tempVal);
		}else if (qName.equalsIgnoreCase("data")) {
			message.setData(tempVal);
		}

	}
	
	public String writeXml(Message message){
		
	    XmlSerializer serializer = Xml.newSerializer();
	    StringWriter writer = new StringWriter();
	    try {
	        serializer.setOutput(writer);
	        serializer.startDocument("UTF-8", true);
	        serializer.startTag("", "message");
	        //serializer.attribute("", "number", String.valueOf(messages.size()));
	        serializer.startTag("", "from");
            serializer.text(message.getSender());
            serializer.endTag("", "from");
            serializer.startTag("", "type");
            serializer.text(message.getType());
            serializer.endTag("", "type");
            serializer.startTag("", "data");
            serializer.text(message.getData());
            serializer.endTag("", "data");
            serializer.endTag("", "message");
	        serializer.endDocument();
	        Log.i("Parser", writer.toString());
	        return writer.toString();        
	    } catch (Exception e) {
	        throw new RuntimeException(e);
	    } 
	}

}
